package com.ctakit.sdk.app.util;

import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 字符串工具类，继承自Apache Commons Lang3 StringUtils
 */
public class StringUtil {

	/**
	 * change a string (like a URL) into a hash string using MD5
	 */
	public static String hashKey(String src) {
		if (src == null) {
			return null;
		}
		String hashKey;
		try {
			final MessageDigest mDigest = MessageDigest.getInstance("MD5");
			mDigest.update(src.getBytes());
			hashKey = bytesToHexString(mDigest.digest());
		} catch (NoSuchAlgorithmException e) {
			hashKey = String.valueOf(src.hashCode());
		}
		return hashKey;
	}

	public static String md5(String src) {
		if (src == null) {
			return null;
		}
		String ret = null;
		try {
			final MessageDigest mDigest = MessageDigest.getInstance("MD5");
			mDigest.update(getBytes(src));
			ret = bytesToHexString(mDigest.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return ret;
	}

	public static String sha1(String data) {
		if (data == null)
			return null;
		String ret = null;
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
			messageDigest.update(getBytes(data));
			ret = bytesToHexString(messageDigest.digest());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	private static String bytesToHexString(byte[] bytes) {
		// http://stackoverflow.com/questions/332079
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < bytes.length; i++) {
			String hex = Integer.toHexString(0xFF & bytes[i]);
			if (hex.length() == 1) {
				sb.append('0');
			}
			sb.append(hex);
		}
		return sb.toString();
	}

	public static String convertTime(long time, String format) {
		if (format == null) {
			format = "yyyy-MM-dd_HHmmss";
		}
		// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat(format, Locale.getDefault());
		Date date = new Date(time);
		return df.format(date);
	}

	/**
	 * Check if the specified string is empty. If an string contains any
	 * character other than ' '(blank space), it is NOT empty
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isEmptyString(String s) {
		if (s == null) {
			return true;
		}
		int length = s.length();
		for (int i = 0; i < length; i++) {
			if (s.charAt(i) != ' ') {
				return false;
			}
		}
		return true;
	}

	public static byte[] getBytes(String s) {
		if (s == null)
			return null;
		try {
			return s.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String fromBytes(byte[] data) {
		String ret = null;
		try {
			if (data != null)
				ret = new String(data, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * 解决空值问题
	 * @param str
	 * @return
	 */
	public static String fixNull(String str) {
		if (TextUtils.isEmpty(str)) {
			str = "";
		}
		return str;
	}

	/**
	 * 解决空值问题
	 * @param value
	 * @return
	 */
	public static BigDecimal fixBigDecimal(BigDecimal value) {
		if (value == null) {
			return BigDecimal.ZERO;
		} else {
			return value;
		}
	}
}
